package com.weiaibenpao.demo.chislim.ui.activity.running;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.weiaibenpao.demo.chislim.R;
import com.weiaibenpao.demo.chislim.base.BaseActivity;
import com.weiaibenpao.demo.chislim.bean.ChinaRankBean;
import com.weiaibenpao.demo.chislim.bean.RankOneBean;
import com.weiaibenpao.demo.chislim.db.preferences.UserPreferences;
import com.weiaibenpao.demo.chislim.presenter.RankPresenter;
import com.weiaibenpao.demo.chislim.ui.adapter.RunRankAdapter;
import com.weiaibenpao.demo.chislim.ui.widget.CircleImage;
import com.weiaibenpao.demo.chislim.utils.Constant;
import com.weiaibenpao.demo.chislim.utils.Utils;
import com.weiaibenpao.demo.chislim.view.RankView;

import butterknife.BindView;
import butterknife.OnClick;

public class ChinaRunRankActivity extends BaseActivity<RankView, RankPresenter> implements RankView,BaseQuickAdapter.RequestLoadMoreListener {


    @BindView(R.id.text_rank)
    TextView textRank;
    @BindView(R.id.img_user_head)
    CircleImage imgUserHead;
    @BindView(R.id.text_user_name)
    TextView textUserName;
    @BindView(R.id.text_user_distance)
    TextView textUserDistance;
    @BindView(R.id.text_user_totalTime)
    TextView textUserTotalTime;
    @BindView(R.id.rank_recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.tv_district)
    TextView tvDistrict;
    @BindView(R.id.tv_no_rank)
    TextView tvNoRank;
    private String code;
    private int userId;
    private RunRankAdapter runRankAdapter;
    private String districtName;
    private final int TOTAL_COUNTER=6;
    private int page=1;
    private int mCurrentCounter;
    private boolean isFirst=true;
    private View emptyView;

    @Override
    public int setLayoutResourceID() {
        return R.layout.activity_china_run_rank;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        userId = UserPreferences.getInstance().getInt(UserPreferences.USER_ID);
        code = getIntent().getStringExtra(Constant.CITY_CODE);
        districtName = getIntent().getStringExtra(Constant.PROVINCE);
        tvDistrict.setText(districtName+"排行榜");
        initAdapter();
        doRequest();
    }

    private void initAdapter() {
        emptyView = LayoutInflater.from(this).inflate(R.layout.layout_empty, null);
        ((TextView) emptyView.findViewById(R.id.tv_empty_new)).setText(districtName+"暂无排名");
        runRankAdapter = new RunRankAdapter(R.layout.rank_item_layout);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(runRankAdapter );
    }

    private void doRequest() {
        /**
         * set fetching on when start network request.
         */
        mPresenter.getRank(userId,page,Constant.DEFAULT_PAGE_SIZE,code);
    }

    @Override
    public RankPresenter initPresenter() {
        return new RankPresenter();
    }

    public static void openActivity(Context context,String code,String district) {
        Intent intent = new Intent(context, ChinaRunRankActivity.class);
        intent.putExtra(Constant.CITY_CODE,code);
        intent.putExtra(Constant.PROVINCE,district);
        context.startActivity(intent);
    }

    @OnClick({R.id.left_image})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.left_image:
                finish();
                break;
        }
    }

    @Override
    public void requestSuccess(ChinaRankBean chinaRankBean) {
        RankOneBean rankOne = chinaRankBean.getRankOne();
        runRankAdapter.setEmptyView(emptyView);

        if (rankOne!=null&&isFirst){
            if (rankOne.getRownum()!=0){
                textRank.setText(rankOne.getRownum()+"");
            }

            Glide.with(this).load(rankOne.getUserImage()).into(imgUserHead);
            textUserName.setText(rankOne.getUserName());
            textUserDistance.setText("总里程"+Utils.decimalFormat(rankOne.getFinishDistance())+"公里");
            textUserTotalTime.setText(Utils.getHmsTime(rankOne.getTotalTime()));
        }
        isFirst=false;

        runRankAdapter.addData(chinaRankBean.getRankList());
        if (chinaRankBean.getRankList().size() < Constant.DEFAULT_PAGE_SIZE) {
            runRankAdapter.loadMoreEnd();
        } else {
            runRankAdapter.loadMoreComplete();
        }

    }


    @Override
    public void onLoadMoreRequested() {
        page++;
        doRequest();
    }
}
